/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       scaled_animation_cache.cpp

	$Header: /game/scaled_animation_cache.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "scaled_animation_cache.h"

#include "platform.h"
#include "scale_bitmap.h"

class t_scaled_animation_cache_data : public t_conversion_cache_data<t_animation_24, t_animation>
{
public:
	// Constructor/Destructor
	t_scaled_animation_cache_data( std::string const & name, double scale );

	void set_origin( t_screen_point origin );
private:
	// Member functions
	virtual t_animation* do_read( std::streambuf & stream, t_progress_handler* handler );

	t_screen_point m_origin;
	double		   m_scale;
};


inline t_scaled_animation_cache_data::t_scaled_animation_cache_data( std::string const & name, double scale )
	:	t_conversion_cache_data<t_animation_24, t_animation>( name ), m_origin( 0, 0 )
{
	m_scale = scale;
}

inline void t_scaled_animation_cache_data::set_origin( t_screen_point origin )
{
	m_origin = origin;
}

// -------------------------------------------------------------------
// cache scales animation to current resolution
// -------------------------------------------------------------------
t_animation* t_scaled_animation_cache_data::do_read( std::streambuf & stream, t_progress_handler* handler )
{
	t_animation*   result = 0;
	t_animation_24 source;

	if ( source.read( stream, handler ))
	{
		t_screen_rect  screen_size = get_main_window_rect();
		t_screen_point origin = m_origin;

		// origin to offset animation corresponds to origin of first frame
		// of animation.
		if (source.size() > 0)
			origin -= source.front().get_rect().top_left();

		source.offset( origin );
		if ( m_scale == 1.0 )
		{
			result = new t_animation( source );
		}
		else
		{
			t_screen_point point = source.get_base_point();

			point.x = point.x * m_scale;
			point.y = point.y * m_scale;
			result = new t_animation;
			result->set_frame_delay( source.get_frame_delay() );
			result->set_frame_order( source.get_frame_order() );
			result->set_base_point( point );
			scale_group( source, *result, m_scale );
		}
	}
	return result;
}

t_scaled_animation_cache::t_scaled_animation_cache( std::string const& name, double scale ) : t_animation_cache( 0 )
{
	m_data = new t_scaled_animation_cache_data( name, scale );
	set( m_data );
}

void t_scaled_animation_cache::set_origin( t_screen_point origin )
{
	m_data->set_origin( origin );
}
